<?php


namespace app\admin\controller\jizhan;

use app\admin\model\HtnsActivity;
use app\admin\model\HtnsCouponIssue;
use app\admin\model\HtnsPageLink;
use app\admin\model\HtnsSystemCity;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;
use think\facade\Cache;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="基站-新UI-首页优惠券配置")
 */
class IndexCoupon extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new HtnsCouponIssue();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index($export = 0)
    {
        if ($this->request->isAjax() || $export) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if ($export) {
                $page  = 1;
                $limit = 10000;
            }
            $where[]          = ['status', '=', 1];
            $where[]          = ['is_del', '=', 0];
            $where[]          = ['receive_type', '=', 1];
            $where[]          = ['remain_count', '>', 0];
            $count = $this->model
                ->where($where)
                ->count();
            $list  = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            // 导出
            if ($export) {
                return $list;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save               = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $where[]          = ['status', '=', 1];
        $where[]          = ['is_del', '=', 0];
        $where[]          = ['receive_type', '=', 1];
        $where[]          = ['remain_count', '>', 0];
        // 提交数据
        if($this->request->isPost()){
//            选中在首页显示的优惠券
            $data = input();
            if($data['coupon']){
                //全部改为0
                $datav['is_show_index'] = 0;
                HtnsCouponIssue::where($where)->update($datav);

                $couponId = array_keys($data['coupon']);
                $where[] = ['id','in',$couponId];
                $datav['is_show_index'] = 1;
                HtnsCouponIssue::where($where)->update($datav);
                $this->success('保存成功');
            }
        }

        $list  = $this->model
            ->where($where)
            ->order($this->sort)
            ->select()->toArray();
        if($list){
            $types = [
                0 => '通用券',
                1 => '品类券',
                2 => '商品券',
            ];
            foreach ($list as &$v) {
                $v['type'] = isset($types[$v['type']]) ? $types[$v['type']] : '';
            }
        }
//        halt($list);
        $this->assign('list', $list);
        return $this->fetch();
    }
}